package com.mvc.web.order.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.web.common.utils.SMSUtil;
import com.mvc.web.common.utils.ToolsUtils;
import com.mvc.web.order.dao.OrderDao;
import com.mvc.web.order.service.OrderService;
import com.mvc.web.order.vo.FileVo;
import com.mvc.web.order.vo.GpsVo;
import com.mvc.web.order.vo.OrderForm;
import com.mvc.web.order.vo.OrderHis;
import com.mvc.web.order.vo.OrderVo;
import com.mvc.web.user.dao.UserDao;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private UserDao userDao;

	@Override
	public List<OrderVo> getList(OrderForm vo) {
		return orderDao.getList(vo);
	}

	@Override
	public long getCount(OrderForm vo) {
		return orderDao.getCount(vo);
	}

	@Override
	public int create(OrderVo vo) {
		int id = orderDao.create(vo);
		//创建历史记录
		vo.setOrderId(id);
		OrderHis oh = new OrderHis();
		oh.setOrderId(id);
		oh.setModifyId(vo.getModifyId());
		oh.setMtime(System.currentTimeMillis()/1000);
		oh.setDescription("创建货运单");
		oh.setType(1);
		orderDao.createHis(oh);
		return id;
	}

	@Override
	public void commit(Integer orderId, int uid) {
		//判断当前车主的手机号是否是用户，不是就新增一条数据
		OrderVo vo = orderDao.getDetail(orderId);
		String carOId = userDao.getCountByPhone(vo.getCarOwnerPhone());
		int carOwnerId=vo.getCarOwnerId();
		if(ToolsUtils.isEmpty(carOId)){
			carOwnerId=userDao.createUser(vo.getCarOwnerPhone(),vo.getCarOwnerName());
			String jsonData="{\"url\":http://huoyunbaba.oss-cn-beijing.aliyuncs.com/zhihjf.apk}";
			SMSUtil.sendSms(jsonData, vo.getCarOwnerPhone(), "T170317001303");
		}else{
			carOwnerId=Integer.parseInt(carOId);
			userDao.updateUserName(vo.getCarOwnerPhone(),vo.getCarOwnerName());
		}
		orderDao.commit(orderId,uid,carOwnerId);
		OrderHis oh = new OrderHis();
		oh.setOrderId(orderId);
		oh.setModifyId(uid);
		oh.setMtime(System.currentTimeMillis()/1000);
		oh.setDescription("提交货运单");
		oh.setType(2);
		orderDao.createHis(oh);
	}

	@Override
	public int getCountForValidate(String phone) {
		return orderDao.getCountForValidate(phone);
	}

	@Override
	public void deleteFile(String fileId) {
		orderDao.deleteFile(fileId);
	}

	@Override
	public void uploadFile(FileVo vo) {
		orderDao.uploadFile(vo);
	}

	@Override
	public void update(OrderVo vo) {
		orderDao.update(vo);
	}

	@Override
	public OrderVo getDetail(Integer orderId) {
		return orderDao.getDetail(orderId);
	}

	@Override
	public int getStatusById(Integer orderId) {
		return orderDao.getStatusById(orderId);
	}

	@Override
	public List<FileVo> getFileList(Integer orderId, int mtype) {
		return orderDao.getFileList(orderId,mtype);
	}

	@Override
	public void delivery(Integer orderId, int uid) {
		//获取订单信息
		OrderVo vo = orderDao.getDetail(orderId);
		OrderHis oh = new OrderHis();
		int status =2;
		//创建者等于操作者，直接结束订单。操作者为车主，已送达
		if(uid==vo.getCreateId()){
			oh.setDescription("货运单已结束");
			oh.setType(4);
			status=4;
		}else if(uid==vo.getCarOwnerId()){
			oh.setDescription("货物已送达");
			oh.setType(3);
			status=3;
		}
		if(status!=2){
			oh.setOrderId(orderId);
			oh.setModifyId(uid);
			oh.setMtime(System.currentTimeMillis()/1000);
			orderDao.createHis(oh);
			orderDao.delivery(orderId,uid,status);
		}
	}

	@Override
	public void gps(String longitude, String latitude, int uid,String address) {
		orderDao.gps(longitude,latitude,uid,address);
	}

	@Override
	public List<GpsVo> getGps(Integer orderId) {
		return orderDao.getGps(orderId);
	}
}
